set more 1

use bgt_employer_firm_07_to_14.dta, clear

keep if inlist(year, 2007, 2010, 2012)

gen educ=edu
replace educ=maxedu if educ==.

gen edureq=(educ!=.)
	
	gen eduhs=(educ==12 & !missing(educ))
	gen edusc=((educ==13 | educ==14) &!missing(educ))
	gen edubd=(educ==16  &!missing(educ))
	gen eduma=(educ==18 &!missing(educ))
	gen eduphd=(educ==21 &!missing(educ))

gen edubdgd=.
replace edubdgd=1 if (edubd==1 | eduma==1 | eduphd==1)
replace edubdgd=0 if (edubd==0 & eduma==0 & eduphd==0)


gen exper=exp
replace exper=maxexp if exp==.

drop edu maxedu exp maxexp

gen expreq=exper!=.
	
	gen exp_0to1=(exper>0 & exper<1 & !missing(exper))
	gen exp_1to2=(exper>=1 & exper<2 &  !missing(exper))
	gen exp_2to3=(exper>=2 & exper<3 & !missing(exper))
	gen exp_3to4=(exper>=3 & exper<4 & !missing(exper))
	gen exp_4to5=(exper>=4 & exper<5 & !missing(exper))
	gen exp_5to7=(exper>=5 & exper<7 & !missing(exper))
	gen exp_7to15=(exper>=7 & exper<15 & !missing(exper))


gen exp2=.
replace exp2=1 if (exp_2to3==1 | exp_3to4==1 | exp_4to5==1 | exp_5to7==1 | exp_7to15==1)
replace exp2=0 if (exp_2to3==0 & exp_3to4==0 & exp_4to5==0 & exp_5to7==0 & exp_7to15==0)

gen exp4=.
replace exp4=1 if (exp_4to5==1 | exp_5to7==1 | exp_7to15==1)
replace exp4=0 if (exp_4to5==0 & exp_5to7==0 & exp_7to15==0)

drop if fips=="na"
count

assert length(fips)==5

drop if inlist(statefip, 11, 72, 66)
count

drop if employerid==.
count

drop if jobid==.


egen empjobid =group(jobid employerid)
replace empjobid=. if employerid==.
replace empjobid=. if jobid==.
egen empjobstateid=group(employerid jobid statefip)


gen occsoc_broad=substr(occsoc,1,2)
gen occsoc_minor=substr(occsoc,1,3)
gen occsoc_detailed=occsoc

destring occsoc_broad, gen(occsoc_broad2)
destring occsoc_minor, gen(occsoc_minor2)
destring occsoc_detailed, gen(occsoc_detailed2)

gen occsoc_broadgroup=2 if occsoc_broad2>=11 & occsoc_broad2<=13
replace occsoc_broadgroup=3 if occsoc_broad2>=15 & occsoc_broad2<=29
replace occsoc_broadgroup=4 if occsoc_broad2>=31 & occsoc_broad2<=39
replace occsoc_broadgroup=5 if occsoc_broad2>=41 & occsoc_broad2<=43
replace occsoc_broadgroup=6 if occsoc_broad2>=45 & occsoc_broad2<=49
replace occsoc_broadgroup=7 if occsoc_broad2>=51 & occsoc_broad2<=53
label variable occsoc_broadgroup "Groups of 2-digit SOC CODES- for HWOL data merge"

egen occyear_broad=group(occsoc_broad2 year)
egen occyear_minor=group(occsoc_minor2 year)
egen occyear_detailed=group(occsoc_detailed2 year)
egen occyear_broadgroup=group(occsoc_broadgroup year)


gen occyear=occyear_broadgroup

merge m:1 statefip year using ur0714.dta, keep(master match) nogen assert(2 3)
drop ne_id state
replace ur=ur/100
drop if ur==.


merge m:1 statefip year occsoc using "data_for_jobtitle_vet13_2.dta", keep(master match)

gen job=1
egen totjob=sum(job), by(occsoc statefip year)
g newweight=occshare_state* (1/totjob)


reghdfe edubdgd ur  if dup_month_st<=50, a(year empjobid) vce(cluster statefip)
reghdfe exp4 ur  if dup_month_st<=50, a(year empjobid) vce(cluster statefip)

reghdfe edubdgd ur  if dup_month_st<=50, a(occyear empjobid) vce(cluster statefip)
reghdfe exp4 ur  if dup_month_st<=50, a(occyear empjobid) vce(cluster statefip)

reghdfe edubdgd ur  if dup_month_st<=50, a(occyear2 empjobid) vce(cluster statefip)
reghdfe exp4 ur  if dup_month_st<=50, a(occyear2 empjobid) vce(cluster statefip)

reghdfe edubdgd ur  if dup_month_st<=50, a(year empjobstateid) vce(cluster statefip)
reghdfe exp4 ur  if dup_month_st<=50, a(year empjobstateid) vce(cluster statefip)

reghdfe edubdgd ur  if dup_month_st<=50, a(occyear empjobstateid) vce(cluster statefip)
reghdfe exp4 ur  if dup_month_st<=50, a(occyear empjobstateid) vce(cluster statefip)

reghdfe edubdgd ur  if dup_month_st<=50, a(occyear2 empjobstateid) vce(cluster statefip)
reghdfe exp4 ur  if dup_month_st<=50, a(occyear2 empjobstateid) vce(cluster statefip)


collapse (mean)  occshare_state educ exper edubdgd edubd exp4 exp2 empjobid empjobstateid ur  (count) job (first) occsoc_broad occsoc_minor occsoc_detailed occyear_broad occyear_broadgroup occyear_minor occyear_detailed occyear occyear2 newweight, by(employerid jobid statefip year)


duplicates tag employerid jobid statefip, gen(dup)
keep if dup


reghdfe edubdgd ur, a(year empjobid) vce(cluster statefip)
reghdfe exp4 ur, a(year empjobid) vce(cluster statefip)

reghdfe edubdgd ur, a(occyear empjobid) vce(cluster statefip)
reghdfe exp4 ur, a(occyear empjobid) vce(cluster statefip)


reghdfe edubdgd ur, a(year empjobstateid) vce(cluster statefip)
reghdfe exp4 ur, a(year empjobstateid) vce(cluster statefip)

reghdfe edubdgd ur, a(occyear empjobstateid) vce(cluster statefip)
reghdfe exp4 ur, a(occyear empjobstateid) vce(cluster statefip)



reghdfe edubdgd ur [w=newweight], a(year empjobstateid) vce(cluster statefip)
reghdfe exp4 ur [w=newweight], a(year empjobstateid) vce(cluster statefip)

reghdfe edubdgd ur [w=newweight], a(occyear empjobstateid) vce(cluster statefip)
reghdfe exp4 ur [w=newweight], a(occyear empjobstateid) vce(cluster statefip)


bys empjobstateid: gen nyear=[_N]
drop if nyear!=3


reghdfe edubdgd ur, a(year empjobid) vce(cluster statefip)
reghdfe exp4 ur, a(year empjobid) vce(cluster statefip)

reghdfe edubdgd ur, a(occyear empjobid) vce(cluster statefip)
reghdfe exp4 ur, a(occyear empjobid) vce(cluster statefip)


reghdfe edubdgd ur, a(year empjobstateid) vce(cluster statefip)
reghdfe exp4 ur, a(year empjobstateid) vce(cluster statefip)

reghdfe edubdgd ur, a(occyear empjobstateid) vce(cluster statefip)
reghdfe exp4 ur, a(occyear empjobstateid) vce(cluster statefip)


reghdfe edubdgd ur [w=job], a(year empjobstateid) vce(cluster statefip)
reghdfe exp4 ur [w=job], a(year empjobstateid) vce(cluster statefip)

reghdfe edubdgd ur [w=job], a(occyear empjobstateid) vce(cluster statefip)
reghdfe exp4 ur [w=job], a(occyear empjobstateid) vce(cluster statefip)






